package com.cll.prototype.sharding.jdbc.keygen;

import org.apache.shardingsphere.core.strategy.keygen.SnowflakeShardingKeyGenerator;
import org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;

/**
 * 描述信息:
 * 基于mysql中内存表实现分布式下主键生成策略
 * @author CLL
 * @version 1.0
 * @date 2020/11/22 10:40
 */
public class MySQLKeyGenerator implements ShardingKeyGenerator {

    private static final Logger logger = LoggerFactory.getLogger(MySQLKeyGenerator.class);

    private Properties properties = new Properties();

    /**
     * TODO 模拟使用，没有实现具体的引用
     */
    private SnowflakeShardingKeyGenerator snowflakeShardingKeyGenerator = new SnowflakeShardingKeyGenerator();

    @Override
    public Comparable<?> generateKey() {
        logger.info("===>>> 使用自定义的MySQL Memory分布式主键生成策略");
        return snowflakeShardingKeyGenerator.generateKey();
    }

    @Override
    public String getType() {
        return "MYSQL";
    }

    @Override
    public Properties getProperties() {
        logger.info("===>>> ger properties");
        return this.properties;
    }

    @Override
    public void setProperties(Properties properties) {
        logger.info("===>> setProperties. properties = {}", properties);
        this.properties = properties;
    }
}
